﻿using System;
using System.Collections.Generic;

namespace _05_ObserverModel_EventDelegate
{
    interface Subject
    {
        String SubjectState { get; set; }
        void Notify();
    }
    delegate void EventHandler();
    class Boss : Subject
    {
        // 申明一事件Update，类型为委托
        public event EventHandler Update;

        public string SubjectState { get; set; }

        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            Update();
        }
    }
    /// <summary>
    /// 前台秘书类
    /// </summary>
    class Secreatary : Subject
    {
        // 申明一事件Update，类型为委托
        public event EventHandler Update;

        public string SubjectState { get; set; }

        /// <summary>
        /// 通知
        /// </summary>
        public void Notify()
        {
            Update();
        }
    }

    abstract class Observer
    {
        public Observer(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }
        public string name;  // 名字
        public Subject sub; // 前台秘书
        public abstract void Update();
    }

    /// <summary>
    /// 看股票的观察者
    /// </summary>
    class StockObserver 
    {
        public StockObserver(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }

        private string name;  
        private Subject sub;

        // 关闭股票行情
        public void CloseStockMarket()
        {
            Console.WriteLine($"{sub.SubjectState},{name}关闭股票行情，继续工作！");
        }
    }

    /// <summary>
    /// 看NBA的观察者
    /// </summary>
    class NBAObserver 
    {
        public NBAObserver(string name, Subject sub)
        {
            this.name = name;
            this.sub = sub;
        }

        private string name;
        private Subject sub;

        // 关闭NBA
        public void CloseNBADirectSeeding()
        {
            Console.WriteLine($"{sub.SubjectState},{name}关闭NBA，继续工作！");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Boss boss = new Boss();
            // 看股票的同事
            StockObserver stockObserver1 = new StockObserver("为观察", boss);
            NBAObserver nbaObserver2 = new NBAObserver("一观察", boss);
            // 前台记下要通知的同事
            boss.Update +=new EventHandler(stockObserver1.CloseStockMarket);
            boss.Update += new EventHandler(nbaObserver2.CloseNBADirectSeeding);
            // 发现老板来了
            boss.SubjectState = "老板来了";
            // 通知
            boss.Notify();

            Console.Read();
        }
    }

}
